package hah;


import javax.swing.*;
import java.util.Stack;

public class ListNode {
      int val;
      ListNode next;
      ListNode() {}
      ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }

    public ListNode deleteDuplicates1(ListNode head) {
        if(head==null){
            return null;
        }
        ListNode rhead=new ListNode();
        rhead.next=head;
        rhead.val=101;
        ListNode pre=rhead;
        ListNode behind=rhead.next;
        int val=-101;
        while (behind!=null){
            if (behind.next!=null&& behind.next.val==behind.val){
                val= behind.val;
                while (behind!=null&&behind.val==val){
                    behind=behind.next;
                }
                pre.next=behind;
            }else {
                behind=behind.next;
                pre=pre.next;
            }
        }
        return rhead.next;
    }

    public ListNode deleteDuplicates(ListNode head) {
          //并不都删除，留下一个
        if (head==null||head.next==null)
            return head;
        if (head.val!=head.next.val){
            head.next=deleteDuplicates(head.next);
        }else {
            ListNode move=head.next;
            while (move!=null&&move.val==head.val)
                move=move.next;
            head.next=move;
        }
        return head;
    }

    //二进制求和
    public String addBinary(String a, String b) {
        char[] a1 = a.toCharArray();
        char[] b1 = b.toCharArray();
        Stack<Integer> stack=new Stack<>();
        int end=Math.min(a1.length,b1.length);
        int flag=0;
        for (int i=1;i<=end;i++){
            int temp=a1[a1.length-i]-'0'+b1[b1.length-i]-'0'+flag;
            if (temp>=2){
                stack.push(temp%2);
                flag=1;
            }else {
                stack.push(temp);
                flag=0;
            }
        }
        if (a1.length<b1.length){
            for (int i=b1.length-end-1;i>=0;i--){
                int temp=b1[i]-'0'+flag;
                if (temp==2){
                    stack.push(0);
                    flag=1;
                }else {
                    stack.push(temp);
                    flag=0;
                }
            }
        }
        if (b1.length<a1.length){
            for (int i=a1.length-end-1;i>=0;i--){
                int temp=a1[i]-'0'+flag;
                if (temp==2){
                    stack.push(0);
                    flag=1;
                }else {
                    stack.push(temp);
                    flag=0;
                }
            }
        }
        if (flag==1){
            stack.push(1);
        }
        StringBuffer sb=new StringBuffer();
        while(!stack.isEmpty()){
            sb.append(stack.pop());
        }
        return sb.toString();
    }
    //旋转链表
    public ListNode rotateRight1(ListNode head, int k) {
        if(head==null||head.next==null||k==0)
            return head;
        ListNode rhead=new ListNode(101);
        rhead.next=head;
        int count=0;
        ListNode p=rhead.next;
        while (p!=null){
            p=p.next;
            count++;
        }
        int rk=k%count;
        while (rk>0){
            change(rhead);
            rk--;
        }
        return rhead.next;
    }
    public void change(ListNode head){
        ListNode p=head;
        while (p.next.next!=null){
            p=p.next;
        }
        p.next.next=head.next;
        head.next=p.next;
        p.next=null;
    }
        //旋转列表另一个解法
        public ListNode rotateRight(ListNode head, int k) {
            if(head==null||head.next==null||k==0)
                return head;
            ListNode p=head;
            int count=1;
            while (p.next!=null){
                p=p.next;
                count++;
            }
            int add=count-k%count;
            if (add==count)
                return head;
            p.next=head;
            while (add-->0){
                p=p.next;
            }
            ListNode res=p.next;
            p.next=null;
            return res;
        }

    public int[] plusOne(int[] digits) {
            /*if (digits.length==0)
                return null;
            int len= digits.length;
            int j=1;
            for (int i=len-1;i>=0;i--){
                int temp=digits[i]+j;
                digits[i]=temp%10;
                j=temp/10;
            }
            if (j==0){
                return digits;
            }else {
                int[] res=new int[digits.length+1];
                res[0]=j;
                for(int i=1;i<=res.length-1;i++){
                    res[i]= digits[i-1];
                }
                return res;
            }*/
        if (digits.length==0)
            return null;
        for (int i= digits.length-1;i>=0;i--){
            if (digits[i]==9){
                digits[i]=0;
            }else {
                digits[i]++;
                return digits;
            }
        }
        int[] res=new int[digits.length+1];
        res[0]=1;
        return res;
    }
    public ListNode removeElements(ListNode head, int val) {
            ListNode rhead=new ListNode();
            rhead.next=head;
            rhead.val=0;
            ListNode p=head;
            while (p.next!=null){
                if (p.next.val==val){
                    p.next=p.next.next;
                }
                p=p.next;
            }
            return rhead.next;
    }

    public static void main(String[] args) {
        String s1="11";
        String s2="1";
        String s = new ListNode().addBinary(s1, s2);
        System.out.println(s);
    }

}
